/**
 * 
 */
package common.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;

/**
 * @author Eric
 *
 */
public class VitalSignDisplay extends JComponent implements ActionListener {

	private static final int READING_FONT_SIZE = 36;

	public static final String ACTION_SET_ALARM = "Set Alarm";
	
	private JLabel label;
	private JLabel reading;
	private JButton setAlarm;

	public VitalSignDisplay( String displayName, Color fontColor ) {
		
		this.setLayout( new BorderLayout() );
		
		label = new JLabel( displayName );
		label.setForeground( fontColor );
		this.add( label, BorderLayout.NORTH );
		
		reading = new JLabel( "------" );
		// Make the font for the reading larger than the other fonts
		Font f = reading.getFont();
		f = new Font( f.getFamily(), f.getStyle(), READING_FONT_SIZE );
		reading.setFont( f );
		reading.setForeground( fontColor );
		this.add( reading, BorderLayout.CENTER );
		
		setAlarm = new JButton();
		setAlarm.setText( 	"<html>Critical High: -<br/>" +
							"High: -<br/>" +
							"Low: -<br/>" + 
							"Critical Low: -</html>" );
		setAlarm.setActionCommand( ACTION_SET_ALARM );
		setAlarm.setForeground( fontColor );
		setAlarm.addActionListener( this );
		this.add( setAlarm, BorderLayout.EAST );
	}
	
	public void addActionListener( ActionListener al ) {
		listenerList.add( ActionListener.class, al );
	}
	
	public void removeActionListener( ActionListener al ) {
		listenerList.remove( ActionListener.class, al );
	}
	
	/**
	 * Sets the reading displayed by this component.  This method should only 
	 * be invoked from the event dispatch thread.
	 * 
	 * @param newReading - the new reading to display
	 */
	public void setReading( String newReading ) {
		reading.setText( newReading );
	}
	
	/**
	 * Sets the alarm limits displayed by this component.  This method should 
	 * only be invoked from the event dispatch thread.
	 * 
	 * @param critLow - Limit for the critical low alarm
	 * @param low - Limit for the low alarm
	 * @param high - Limit for the high alarm
	 * @param critHigh - Limit for the critHigh alarm
	 */
	public void setAlarmLimits( String critLow, String low, String high, String critHigh ) {
		setAlarm.setText( 	"<html>Critical High: " + critHigh + "<br/>" +
				"High: " + high + "<br/>" +
				"Low: " + low + "<br/>" + 
				"Critical Low: " + critLow + "</html>" );
	}
	
	/**
	 * Sets the font color for the text displayed by this component.  This 
	 * method should only be invoked from the event dispatch thread.
	 * 
	 * @param newColor - the new font color to use
	 */
	public void setForeground( Color newColor ) {
		label.setForeground( newColor );
		reading.setForeground( newColor );
		setAlarm.setForeground( newColor );
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		ActionListener[] listeners = listenerList.getListeners( ActionListener.class );
		if ( listeners.length > 0 ) {
			ActionEvent ae = new ActionEvent( this, e.getID(), 
												e.getActionCommand(), 
												e.getWhen(), 
												e.getModifiers() );
			for ( ActionListener al : listeners ) {
				al.actionPerformed( ae );
		}
		}
	}
}
